$(function () {

	var error = new Error();

	$('.i-checks').iCheck({
		checkboxClass: 'icheckbox_square-green',
		radioClass: 'iradio_square-green',
	});

	//自定义校验规则
	$.validator.addMethod('phoneNumber', function (value, element, param) {
		return /^1[3|4|5|8][0-9]\d{4,8}$/.test(value);
	}, '<span class="fa fa-times-circle"></span> 手机号码不正确');

    var oDataSource = {
        /**
         * 用户登陆
         * @param oData:"account","password"
         * @return {Object} Deferred对象
         */
        login: function (oData) {
            return $.ajax({
				//url:baseUrl+"/sysUser/login",
				url: Site("user.login"),
                method: "post",
                data: {data: JSON.stringify(oData)}
            });
        }
    };

	$('#loginform').validate({
		rules: {
			account: {
				required: true,
				phoneNumber: ''
			},
			password: "required"
		},
		messages: {
			account: {required:"账号或密码不能为空"},
			password: {required:"账号或密码不能为空"},
		},
		errorPlacement: function (error, element) {
			$("#error").html(error);
		},
		errorElement: "small",
		errorClass: 'text-danger',
		submitHandler: send
	});

	/*登录*/
	function send() {
		var $btnLogin = $('#loginform button[type="submit"]');
		if($btnLogin.hasClass('disabled')) {
			return;
		}
		$btnLogin.addClass('disabled');
        var account = $("#account").val();
        var password = $("#password").val();

		var oParam = {account:account,password:password};
		console.log(oParam)
		var done = function(data){
			console.log("登录.done:",data)
			if(data.code == 0){
                $("#error").html("账号或密码错误，请重试！");
                $('#loginform button[type="submit"]').removeClass("disabled");
			}else if(data.code == 1){
				sessionStorage.setItem("account", JSON.stringify(data));
                //如果需要每次登陆成功都加载首页, 下面一句则改成location = "main.html";
                location = "main.html" + location.hash;
			}
			//级别 data.level 0为平台  1为商户
		};
		var failed = function(e){
			error.listen(e.state,"登录失败，请重试！");
		};
        oDataSource
			.login(oParam)
			.always(function(data) {
				$btnLogin.removeClass('disabled');
				return data;
			})
			.then(done,failed);
	}

	/*判断是否记住密码*/
	if(localStorage.mindpsd == "true"){
		$("#account").val(localStorage.account);
		$("#password").val(localStorage.psd);
		$('input').iCheck('check');   //将输入框的状态设置为checked
	}

	/*监听记住密码CheckBox选中*/
	$('input').on('ifChecked', function(event){ //ifCreated 事件应该在插件初始化之前绑定
		//alert(event.type + ' callback');
		localStorage.mindpsd = true;
		var account = $("#account").val();
		var psd = $("#password").val();
		localStorage.psd = psd;
		localStorage.account = account;
		// console.log("记住密码！"+psd);
	});
	/*监听记住密码CheckBox移除选中*/
	$('input').on('ifUnchecked', function(event){ //ifCreated 事件应该在插件初始化之前绑定
		localStorage.clear();
	});

	// function lg(account,password){
     //    if (md5(account) === acc && md5(password) === psd){
     //        sessionStorage.setItem("account", JSON.stringify({account:account}));
     //        location = "main.html";
     //    } else {
     //        alert("账号或密码错误，请重试！");
     //        $('#loginform button[type="submit"]').removeClass("disabled");
     //        return false;
     //    }
	// }
});